import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by luoxf on 2018/8/22.
 */
public class Htmlunit {

    public static void main(String[] args) {

        WebClient wc = new WebClient(BrowserVersion.FIREFOX_52);
        wc.getOptions().setJavaScriptEnabled(true); //启用JS解释器，默认为true
        wc.setJavaScriptTimeout(100000);//设置JS执行的超时时间
        wc.getOptions().setCssEnabled(false); //禁用css支持
        wc.getOptions().setThrowExceptionOnScriptError(false); //js运行错误时，是否抛出异常
        wc.getOptions().setTimeout(10000); //设置连接超时时间 ，这里是10S。如果为0，则无限期等待
        wc.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX

        try {
            HtmlPage page = wc.getPage("http://v.douyin.com/dq7JB1");
            FileWriter fileWriter = null;

            fileWriter = new FileWriter("D:\\text.html");

            String str = "";
            //获取页面的XML代码
            str = page.asXml();
            fileWriter.write(str);
            //关闭webclient
            wc.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
